package code._16reflect._08proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//代理模式分析
public class ProxyTest {
    public static void main(String[] args) {
        UserImpl user = new UserImpl();
        User proxy = (User) Proxy.newProxyInstance(user.getClass().getClassLoader(),
                user.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("verigy");
                        Object invoke = method.invoke(user, args);
                        System.out.println("log");
                        return invoke;
                    }
                });
    }
}

class MyInvocationHandler implements InvocationHandler{
    UserImpl user=new UserImpl();
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 身份验证
        System.out.println("verify");
        //启动方法
        Object invoke = method.invoke(user, args);

        // 日志记录
        System.out.println("log");
        return invoke;
    }
}
